﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL.Comment;
using BLL.Comment.Impl;
using Model;
using BLL.Users;
using BLL.Users.Impl;
using GenericLibrariesToolKit;
using Utils;
using System.Text;

public partial class Page_CommentSite_CommentOfList : System.Web.UI.Page
{
    SiteCommentBO siteCommentBO = new SiteCommentBOImpl();
    protected void Page_Load(object sender, EventArgs e)
    {

            SiteCommentDataBind();
            UIControl();
    }

    private void UIControl()
    {
        if (WebUtil.GetIdentifier(Session["userId"]) == "")
        {
            StringBuilder script = new StringBuilder();
            script.Append("<script>");
            script.Append("document.getElementById('PostComment').style.display='none';");
            script.Append("</script>");
            Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", script.ToString());
        }       
    }


    private void SiteCommentDataBind()
    {
        //设置orderby查询条件
        Dictionary<string, string> orderBy = new Dictionary<string, string>();
        orderBy.Add("SiteCommentDate", "desc");

        QueryResult<SiteComment> querySiteComments = siteCommentBO.GetScrollData(-1,-1,null,orderBy);
        List<SiteComment> listSiteComments = querySiteComments.ResultList;
        GVSiteComment.DataSource = listSiteComments;
        GVSiteComment.DataBind();
    }

    protected void LBCommentDetail_Click(object sender, EventArgs e)
    {
        LinkButton btn=(LinkButton)sender;
        string siteCommentId=btn.CommandArgument.ToString();
        Response.Redirect("CommentDetails.aspx?commentId=" + siteCommentId);
    }
    protected void LBPoster_Click(object sender, EventArgs e)
    {
        LinkButton bt = (LinkButton)sender;
        string userId = bt.CommandArgument.ToString();
        UserManageBO userManageBO = new UserManageBOImpl();
        string url = userManageBO.ShowUserDetail(userId);
        Response.Redirect(url);
    }
    protected void btnCommentSubmit_Click(object sender, ImageClickEventArgs e)
    {
        string siteCommentDetail = txtComment.Text.Trim();
        SiteComment siteComment = new SiteComment()
        {
            SiteCommentDetail = siteCommentDetail,
            UserId=Session["userId"].ToString(),
        };
        bool result = siteCommentBO.Add(siteComment);
        if (result)
        {
            Response.Redirect(Request.Url.ToString()); 
        }
        else
        {
            Response.Write("<script>alert('Comments fail!')</script>");
        }
    }
    //分页处理
    protected void GVSiteComment_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GVSiteComment.PageIndex = e.NewPageIndex;
        this.GVSiteComment.DataBind();
    }
    protected void btnCommentsBack_Click(object sender, ImageClickEventArgs e)
    {

        Response.Write("<script language=javascript>history.go(-2);</script>");
    }
}